package com.game.majiang.vo;

import java.sql.SQLException;
import java.sql.Types;

import javax.sql.rowset.CachedRowSet;

import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.SqlHelperwrite;

public class FriendsVo {
	
	public static int Addfriends(int uid,int gid,String uname) {
		boolean f=false;
		int i=0;
		String cmdText1 = "select * from friends where  uid=? and guanzhuid=?";
		Parameter p1 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
		Parameter p2 = new Parameter(Types.INTEGER, gid, ParameterType.Input);
		CachedRowSet rs1 =SqlHelper.getResultSet(cmdText1,p1,p2);
		try {
			if (rs1.next()) 
			{
				f=true;
				i=2;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			
		}
		if(!f) {
	
			String cmdText = "insert into friends(uid,guanzhuid,name) values(?,?,?)";
			Parameter p3 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
			Parameter p4 = new Parameter(Types.INTEGER, gid, ParameterType.Input);
			Parameter p5 = new Parameter(Types.VARCHAR, uname, ParameterType.Input);
			SqlHelperwrite.execute(cmdText,p3,p4,p5);
		    i=1;
		}
		return i;
	
	}
	public static int Iffriends(int uid,int gid) {
		int f=0;
		String cmdText1 = "select * from friends where  uid=? and guanzhuid=?";
		Parameter p1 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
		Parameter p2 = new Parameter(Types.INTEGER, gid, ParameterType.Input);
		CachedRowSet rs1 =SqlHelper.getResultSet(cmdText1,p1,p2);
		try {
			if (rs1.next()) 
			{
				f=1;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			
		}
		return f;
	}
	public static int friendscount(int uid) {
		int f=0;
		String cmdText1 = "select * from friends where  uid=? ";
		Parameter p1 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
		CachedRowSet rs1 =SqlHelper.getResultSet(cmdText1,p1);
		try {
			while (rs1.next()) 
			{
				f++;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			
		}
		return f;
	}
	public static void Deletefriends(int id) {
		String cmdText = "DELETE FROM friends WHERE id IN ("+id+") ";
		SqlHelperwrite.execute(cmdText);	
	}
	public static void Updatefriends(String bname,int id) {
		String cmdText = "UPDATE friends set bname=? WHERE id=?";
		Parameter p1 = new Parameter(Types.VARCHAR,bname, ParameterType.Input);
		Parameter p2 = new Parameter(Types.INTEGER, id, ParameterType.Input);
		SqlHelper.execute(cmdText, p1,p2);
	}
	public static int guanzhucount(int uid) {
		int i=0;
		String cmdText = "select count(id) from friends where uid=? ";
		Parameter p = new Parameter(Types.INTEGER, uid, ParameterType.Input);
		CachedRowSet rs =SqlHelper.getResultSet(cmdText,p);
		try {
			if (rs.next()) 
			{
				i=rs.getInt(1);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			
		}
		return i;
	}
	

}
